package com.mmp.common.servlet;

//import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.FontFactory;
//import com.itextpdf.text.Paragraph;
//import com.itextpdf.text.html.simpleparser.HTMLWorker;
//import com.itextpdf.text.pdf.FdfWriter;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;

//@WebServlet("/PdfServlet")
public class PdfServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		FontFactory.registerDirectories();
		Document document = new Document();
		// step 2
		ServletOutputStream out = response.getOutputStream();
		PdfWriter writer;
		try {

			FontFactory.register("C:\\Windows\\Fonts\\ARIALUNI.TTF", "Arial Unicode MS");
			// FontFactory.registerFamily(FontFactory.TIMES, FontFactory.TIMES,
			// path);
			FontFactory.register("C:\\Windows\\Fonts\\andlso.ttf");
			// BaseFont bf = BaseFont.createFont(FontFactory.TIMES,
			// BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			// Font f = new Font(bf, 10);
			response.setContentType("application/pdf");
			writer = PdfWriter.getInstance(document, response.getOutputStream());
			writer.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
			// writer.setLanguage(PdfWriter.);
			// step 3
			document.open();
			// step 4

			String style =
			// "<style>/* tables */" +
			// "table {" +
			// "	font-family:arial;" +
			// "	background-color: #CDCDCD;" +
			// "	margin:10px 0pt 15px;" +
			// "	font-size: .9em;" +
			// "	width: 100%;" +
			// "	text-align: right;" +
			// "}" +
			// "table thead tr th, table.tablesorter tfoot tr th {" +
			// "	background-color: #e6EEEE;" +
			// "	border: 1px solid #FFF;" +
			// "	/*font-size: 8pt;*/" +
			// "	padding: 4px;" +
			// "}" +
			// "table thead tr .header {" +
			// "	background-image: url(bg.gif);" +
			// "	background-repeat: no-repeat;" +
			// "	background-position: center left;" +
			// "	cursor: pointer;" +
			// "}" +
			// "table tbody td {" +
			// "	color: #3D3D3D;" +
			// "	padding: 4px;" +
			// "	background-color: #FFF;" +
			// "	vertical-align: top;" +
			// "}" +
			// "table tbody tr.odd td {" +
			// "	background-color:#F0F0F6;" +
			// "}" +
			// "table thead tr .headerSortUp {" +
			// "	background-image: url(asc.gif);" +
			// "}" +
			// "table thead tr .headerSortDown {" +
			// "	background-image: url(desc.gif);" +
			// "}" +
			// "table thead tr .headerSortDown, table.tablesorter thead tr .headerSortUp {"
			// +
			// "    background-color: #8dbdd8;" +
			// "}" +
			// "</style>" +
			"<link type=\"text/css\" rel=\"stylesheet\" href=\"http://localhost:8080/MMP/js/jquery.tablesorter/themes/blue/style_pdf.css\"></link>";

			String html = ""
					+ "<meta http-equiv=\"content-type\" content=\"text/html\";charset=\"utf-8\" />"
					+ ""
					+ ""
					+ "<table id=\"sp_list\" class=\"tablesorter\" style=\"dir:rtl\">"
					+ "		<thead>"
					+ "			<tr>"
					+ "				<th>م</th>"
					+ "				<th class=\"header\">اعوض</th>"
					+ "				<th class=\"header\" style=\"font-family:arial\">الاسم</th>"
					+ "				<th class=\"header\">الكمية المتاحة</th>"
					+ "				<th class=\"header\">الحد الأدنى</th>"
					+ "				<th class=\"header\">متوسط سعر القطعة بالريال السعودي</th>"
					+ "			</tr>"
					+ "		</thead>"
					+ "		<tbody>"
					+ "			"
					+ "			<tr class=\"odd\">"
					+ "				<td align=\"center\">1</td>"
					+ "				<td><a href=\"/MMP/SearchForSPCategoryProcess?dest=update_spcategory&amp;code=abc\" target=\"_blank\"> abc</a></td>"
					+ "	                            "
					+ "				<td>فيش كهرباء</td>"
					+ "	            <td>10</td>"
					+ "	            <td>10</td>"
					+ "	            <td>5.50</td>     "
					+ "			</tr>"
					+ "			"
					+ "			<tr class=\"even\">"
					+ "				<td align=\"center\">2</td>"
					+ "				<td><a href=\"/MMP/SearchForSPCategoryProcess?dest=update_spcategory&amp;code=def\" target=\"_blank\"> def</a></td>"
					+ "	                            " + "				<td>مكثفات</td>" + "	            <td>80</td>"
					+ "	            <td>50</td>" + "	            <td>0.20</td>     " + "			</tr>" + "			"
					+ "		</tbody>" + "	</table>" + "	" + "" + "      ";
			// "	</body>" +
			// "</html>";

			html = style + html;
			XMLWorkerHelper.getInstance().parseXHtml(writer, document, new StringReader(html));

			document.addAuthor("Awad Khalil Auther");
			document.addCreator("awad Creator");
			document.addSubject("Awad Subject");
			document.addTitle("Awad Title");
			document.close();
			PdfWriter.getInstance(document, out);

			System.out.println("PDF Created!-----------");

		} catch (DocumentException e) {

			e.printStackTrace();
		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		doGet(request, response);
	}

}
